package com.bitejiuyeke.biteadminservice.house.controller;

import com.bitejiuyeke.biteadminapi.house.domain.dto.SearchHouseListReqDTO;
import com.bitejiuyeke.biteadminapi.house.domain.vo.HouseDetailVO;
import com.bitejiuyeke.biteadminapi.house.feign.HouseFeignClient;
import com.bitejiuyeke.biteadminservice.house.domain.dto.*;
import com.bitejiuyeke.biteadminservice.house.domain.vo.HouseVO;
import com.bitejiuyeke.biteadminservice.house.service.IHouseService;
import com.bitejiuyeke.bitecommoncore.domain.dto.BasePageDTO;
import com.bitejiuyeke.bitecommoncore.utils.BeanCopyUtil;
import com.bitejiuyeke.bitecommondomain.domain.R;
import com.bitejiuyeke.bitecommondomain.domain.vo.BasePageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/house")
@Slf4j
public class HouseController implements HouseFeignClient {

    @Autowired
    private IHouseService houseService;

    /**
     * 添加或编辑房源
     *
     * @param houseAddOrEditReqDTO 请求参数
     * @return 响应结果
     */
    @PostMapping("/add_edit")
    public R<Long> addOrEdit(@Validated @RequestBody HouseAddOrEditReqDTO houseAddOrEditReqDTO){
        Long HouseId = houseService.addOrEdit(houseAddOrEditReqDTO);
        return R.ok(HouseId);
    }

    /**
     * 获取房源详情
     *
     * @param houseId 房源ID
     * @return 响应结果
     */
    @Override
    public R<HouseDetailVO> detail(Long houseId) {
        return R.ok(houseService.detail(houseId).convertToVO());
    }

    /**
     * 搜索房源列表，支持翻页筛选
     *
     * @param houseListReqDTO 搜索参数
     * @return 响应结果
     */
    @PostMapping("/list")
    public R<BasePageVO<HouseVO>> list(@Validated @RequestBody HouseListReqDTO houseListReqDTO){
        BasePageDTO<HouseDescDTO> houseDescList = houseService.list(houseListReqDTO);
        BasePageVO<HouseVO> result = new BasePageVO<>();
        BeanUtils.copyProperties(houseDescList, result);
        return R.ok(result);
    }


    @PostMapping("/status/edit")
    public R<?> editStatus(@Validated @RequestBody HouseStatusEditReqDTO houseStatusEditReqDTO){
        houseService.editStatus(houseStatusEditReqDTO);
        return R.ok();
    }

    @GetMapping("/refresh")
    public R<Void> refreshHouseIds(){
        houseService.refreshHouseIds();
        return R.ok();
    }


    @Override
    public R<BasePageVO<HouseDetailVO>> searchList(@Validated @RequestBody SearchHouseListReqDTO searchHouseListReqDTO){
        BasePageVO<HouseDetailVO> result = new BasePageVO<>();
        BasePageDTO<HouseDTO> searchDTO =  houseService.searchList(searchHouseListReqDTO);
        result.setTotals(searchDTO.getTotals());
        result.setTotalPages(searchDTO.getTotalPages());
        result.setList(BeanCopyUtil.copyListProperties(searchDTO.getList(), HouseDetailVO::new));
        return R.ok(result);
    }


}